/*
	订单管理:发货管理
 */
$(function () {
    $("#jqGrid").Grid({
        url: '../logistics/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '物流编号', name: 'logisticsNo', index: 'logisticsNo', width: 40},
			{label: '订单编号', name: 'orderno', index: 'orderno', width: 50},
			{label: '用户编号', name: 'userId', index: 'user_id', width: 30},
			{label: '用户名', name: 'userName', index: 'user_name', width: 40},
			{label: '手机号', name: 'phone', index: 'phone', width: 40},
			{label: '产品名称', name: 'pname', index: 'pname', width: 40},
			{label: '收货人', name: 'receiveUser', index: 'receiveUser', width: 50},
			{label: '数量', name: 'num', index: 'num', width: 20},
			{label: '订单类型', name: 'type', index: 'type', width: 50,
				formatter: function (value) {
					if (value == '1') {
						return '商城购买订单';
					} else if (value == '2') {
						return 'SPC兑换订单';
					}else if (value == '3') {
						return '奖金兑换订单';
					}
					return value;
				}
			},
			{label: '状态', name: 'statu', index: 'statu', width: 30,
				formatter: function (value) {
					if (value == '1') {
						return '已发货';
					} else if (value == '2') {
						return '已收货';
					}
					return value;
				}
					},
			{label: '备注', name: 'remark', index: 'remark', width: 50},
			{label: '下单时间', name: 'orderDate', index: 'orderDate', width: 50},
			{label: '发货日期', name: 'createDate', index: 'create_date', width: 50}

		]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		logistics: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    pname:'',
			phone:'',
			orderno:'',
			type:'',
			date:''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.logistics = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.logistics.id == null ? "../logistics/save" : "../logistics/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.logistics),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../logistics/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../logistics/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.logistics = r.logistics;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'pname': vm.q.pname ,'phone':vm.q.phone ,'orderno':vm.q.orderno,"orderType":vm.q.type,'date':vm.q.date==''?'':new Date(vm.q.date).dateFormat("yyyy-MM-dd")},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
				pname:'',
				phone:'',
				orderno:'',
				type:'',
				date:''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});